package com.chennan.javabase.domain.jdk8;

import com.google.common.collect.Lists;
import lombok.Data;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author chennan
 * @createTime 2021年10月27日 15:44:00
 */
public class Testgroupby {
    public static void main(String[] args) {
        Product prod1 = new Product(1L, 1, new BigDecimal("2"), "面包", "零食");
        Product prod2 = new Product(2L, 1, new BigDecimal("2"), "饼干", "零食");
        Product prod3 = new Product(3L, 1, new BigDecimal("3"), "月饼", "零食");
        Product prod4 = new Product(4L, 1, new BigDecimal("1"), "青岛啤酒", "啤酒");
        Product prod5 = new Product(5L, 1, new BigDecimal("1"), "百威啤酒", "啤酒");
        List<Product> prodList = Lists.newArrayList(prod1, prod2, prod3, prod4, prod5);
        for (Product product : prodList) {
            product.setId(8L);
        }
//        Map<String, List<Product>> collect = prodList.stream().collect(Collectors.groupingBy(Product::getCategory));

        boolean b = prodList.stream().noneMatch(item -> item.getNum().equals(0));
        Product reduce = prodList.stream().reduce(new Product(),Product::add);
        System.out.println(reduce+"cess"+(b?1:0));
    }
}
@Data
class Product {
    private Long id;
    private Integer num;
    private BigDecimal price = BigDecimal.ZERO;
    private String name;
    private String category;
    public Product(){}
    public Product(Long id, Integer num, BigDecimal price, String name, String category) {
        this.id = id;
        this.num = num;
        this.price = price;
        this.name = name;
        this.category = category;
    }
    Product add(Product product){
        this.price=this.price.add(product.getPrice());
        return this;
    }

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", num=" + num +
                ", price=" + price +
                ", name='" + name + '\'' +
                ", category='" + category + '\'' +
                '}';
    }
}



